<template>
  <div class="channel-edit__container">
    <!-- 弹出层的头部区域 -->
    <van-nav-bar title="频道管理">
      <template #right>
        <van-icon
          name="cross"
          size="0.37333334rem"
          color="white"
          @click="closePopupFn"
        />
      </template>
    </van-nav-bar>
    <!-- 我的频道 -->
    <div class="my-channel-box">
      <div class="channel-title">
        <span
          >我的频道
          <span class="small-title">
            点击{{ isShow ? '删除' : '进入' }}频道
          </span>
        </span>
        <span @click="isShow = !isShow">{{ isShow ? '完成' : '编辑' }}</span>
      </div>
      <!-- 我的频道列表 -->
      <van-row type="flex">
        <van-col
          span="6"
          v-for="(obj, index) in selectedChannelList"
          :key="obj.id"
          @click="$emit('switch-channel', obj.id)"
        >
          <div class="channel-item van-hairline--surround">
            {{ obj.name }}
            <!-- 删除的徽标 -->
            <van-badge
              color="transparent"
              class="cross-badge"
              v-if="isShow && obj.id !== 0"
            >
              <template #content>
                <van-icon
                  name="cross"
                  class="badge-icon"
                  color="#cfcfcf"
                  size="0.32rem"
                  @click="delChannelFn(index)"
                />
              </template>
            </van-badge>
          </div>
        </van-col>
      </van-row>
    </div>

    <!-- 更多频道 -->
    <div class="more-channel-box">
      <div class="channel-title">
        <span>点击添加更多频道：</span>
      </div>
      <!-- 更多频道列表 -->
      <van-row type="flex">
        <van-col
          span="6"
          v-for="obj in unselectedChannelList"
          :key="obj.id"
          @click="addChannelFn(obj)"
        >
          <div class="channel-item van-hairline--surround">{{ obj.name }}</div>
        </van-col>
      </van-row>
    </div>
  </div>
</template>

<script>
// 目标：展示已选择的频道列表
// 1. 父传子
// 2.v-for -> van-col(列)

// 目标：点击编辑按钮切换状态
// 1. 自身文字切换
// 2.进入或删除频道文字切换
// 3.X徽标的切换
// 4.通过一个布尔变量控制即可，因为三处是一起变的
import { getAllChannelListAPI } from '@/api/Home'
export default {
  name: 'ChannelEdit',
  props: {
    // 父传子 -> 用户选择的频道列表
    selectedChannelList: {
      type: Array,
      required: true
    }
  },
  data () {
    return {
      allChannelList: [], // 所有的频道列表
      isShow: false // 控制X的可见性
    }
  },
  async created () {
    const {
      data: { channels }
    } = await getAllChannelListAPI()
    this.allChannelList = channels
  },
  computed: {
    // 未选择的频道列表
    unselectedChannelList () {
      // 1. 对allChannelList进行过滤-> filter
      // 2.判断obj在不在selectedChannelList数组中，如果不在，则留下；否则干掉
      // some / find / findIndex，注意：不能用includes判断，includes只能检测基本类型
      return this.allChannelList.filter((obj) => {
        // if (this.selectedChannelList.some((item) => item.id === obj.id)) {
        //   return false
        // } else {
        //   return true
        // }
        // 等同于下面
        return !this.selectedChannelList.some((item) => item.id === obj.id)
      })
    }
  },
  methods: {
    // 添加频道 -> 子传父
    addChannelFn (obj) {
      if (!this.isShow) {
        this.$toast.fail('请点击编辑')
        return
      }
      this.$emit('add-channel', obj)
    },
    // 删除频道 -> 子传父
    delChannelFn (index) {
      this.$emit('del-channel', index)
    },
    closePopupFn () {
      this.$emit('close-popup')
      setTimeout(() => {
        this.isShow = false
      }, 500)
    }
  }
}
</script>

<style scoped lang="less">
.van-popup,
.popup-container {
  background-color: transparent;
  height: 100%;
  width: 100%;
}

.popup-container {
  display: flex;
  flex-direction: column;
}

.pop-header {
  height: 90px;
  background-color: #007bff;
  color: white;
  text-align: center;
  font-size: 14px;
  position: relative;
  .title {
    width: 100%;
    position: absolute;
    bottom: 15px;
  }
}

.pop-body {
  flex: 1;
  overflow: scroll;
  padding: 8px;
  background-color: white;
}

.my-channel-box,
.more-channel-box {
  .channel-title {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    line-height: 28px;
    padding: 0 6px;
  }
}

.channel-item {
  font-size: 12px;
  text-align: center;
  line-height: 36px;
  background-color: #fafafa;
  margin: 5px;
}

/*删除的微标 */
.cross-badge {
  position: absolute;
  right: -3px;
  top: 0;
  border: none;
}

/*提示文字 */
.small-title {
  font-size: 10px;
  color: gray;
}
</style>
